<?php

namespace App\Http\Resources;

use App\Models\JobpositionModel;
use App\Models\PlazaModel;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Collection;

class PlazaListResource extends ResourceCollection
{

    /**
     * Transform the resource into an array.
     */
    public function toArray($request)
    {
        return [
            'item_list' => $this->collectionList($this->collection),
            'total' => $this->total(),
            'page' => $this->currentPage(),
            'count' => $this->count(),
            'is_next' => $this->hasMorePages(),
            'last_page' => $this->lastPage(),
        ];
    }

    private function collectionList(Collection $collection)
    {
        $list = [];
        foreach ($collection as $item) {
            $list[] = [
                'id' => $item->id,
                'cur_type' => $item->cur_type,
                'job_position_id' => $item->job_position_id ?? 0,
                'images' => $item->images ?? '',
                'area_images' => $item->cur_type == PlazaModel::TYPE_AREA ? $item->area_images : '',
                'name' => $item->cur_type == PlazaModel::TYPE_AREA ? $item->name : $item->jobPositionInfo->position_name,
                'tags' => $item->tags??[],
                'salary' => $item->cur_type == PlazaModel::TYPE_AREA ? '' : $item->jobPositionInfo->salary,
                'salary_type' => $item->cur_type == PlazaModel::TYPE_AREA ? '' : JobpositionModel::SALARY_TYPE_LIST[$item->jobPositionInfo->salary_type],
                'apply_num'=>$item->applyNum(),
            ];
        }
        return $list;
    }
}
